package uniandes.dermatologia;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import uniandes.dermatologia.singleton.DermatologiaUniandesApp;
import uniandes.dermatologia.training.CaseLogic;
import uniandes.dermatologia.training.TestLogic;
import uniandes.dermatologia.training.pojos.Attempt;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestDetailActivity extends Activity {
	
	private TestLogic testLogic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_detail);
		DermatologiaUniandesApp app = (DermatologiaUniandesApp)getApplication();
		testLogic = app.getCurrentTestLogic();
		TextView txtTitleTest = (TextView)findViewById(R.id.txtTestTitle);
		txtTitleTest.setText(testLogic.getName());
		TextView txtTestDescription = (TextView)findViewById(R.id.txtTestDescr);
		txtTestDescription.setText(testLogic.getDescription());
		
		Date endDate = testLogic.getEnd_date();
		
		String dateStr = new SimpleDateFormat(DermatologiaUniandesApp.DATE_PATTERN, Locale.US).format(endDate);
		
		TextView txtEndDate = (TextView)findViewById(R.id.txtEndDate);
		txtEndDate.setText(dateStr);
		
		TextView txtNumCases = (TextView)findViewById(R.id.txtNumCases);
		
		
		ArrayList<CaseLogic> caseLogics = testLogic.getCaseLogics(); 
		
		int numQ = 0, num = caseLogics.size();
		if(num > 0)
		{
			txtNumCases.setText(String.valueOf(num));
			for(CaseLogic c: caseLogics)
			{
				numQ += c.getQuestions().size();
			}
		}
		else
		{
			txtNumCases.setVisibility(View.GONE);
		}
		
		numQ += testLogic.getQuestions().size();
		
		TextView txtNumQuestions = (TextView)findViewById(R.id.txtNumQuestions);
		
		txtNumQuestions.setText(String.valueOf(numQ));
		
		
		Button btnBeginTest = (Button)findViewById(R.id.btnViewTest);
		btnBeginTest.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DermatologiaUniandesApp app =  (DermatologiaUniandesApp)getApplication();
				Attempt attempt = new Attempt();
				attempt.setMytest_id(testLogic.getId());
				attempt.setStudent_id(app.getUserId());
				app.setAttempt(attempt);
				app.startTimer();
				if(testLogic.isWithCases())
				{
					Intent intent = new Intent(TestDetailActivity.this, CaseDescription.class);
					startActivity(intent);
				}
				else
				{
					app.setCurrentQuestionIndex(0);
					app.setCurrentCaseIndex(-1);
					app.setCurrentCaseLogic(null);
					Intent intent = new Intent(TestDetailActivity.this, QuestionActivity.class);
					startActivity(intent);
				}
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test_detail, menu);
		return true;
	}

}
